package cn.smxy.ln.controller;

import cn.smxy.ln.common.lang.Result;
import cn.smxy.ln.common.utils.RedisUtil;
import cn.smxy.ln.pojo.*;
import cn.smxy.ln.service.*;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ln
 * @date 2022年05月24日 19:25
 */
@RestController
@RequestMapping("/project")
public class ProjectController {

    @Autowired
    ProjectService projectService;

    @Autowired
    WorkService workService;

    @Autowired
    WorkContentService workContentService;

    @Autowired
    WorkProveService workProveService;

    @Autowired
    WorkRegisterService workRegisterService;

    @Autowired
    RedisUtil redisUtil;

    @GetMapping("/list")
    @Cacheable(cacheNames = "project", unless = "#result.getData() == null")    //key:缓存数据时使用的 key，可以用它来指定。默认是使用方法参数的值；cacheNames/value：二者选其一即可，指定缓存组件的名字
    @HystrixCommand(fallbackMethod = "hystrixGetList")
    public Result queryAll() {
        List<Project> list = projectService.list();
        if (list == null) {
            throw new RuntimeException("数据库没有project数据，或者信息无法找到");
        }
        return Result.succ(list);
    }

    public Result hystrixGetList() {
        return Result.fail("没有得到对应的信息，数据库project是空的，或者刷新试试");
    }

    @PostMapping("/save")
    @Transactional
    public Result save(@RequestBody Project project) {
        if ((project.getName() == null || project.getName().equals("")) && (project.getMemo() == null || project.getMemo().equals("")))
            return Result.fail("project为空，保存失败");
        if (project.getName() == null || project.getName().equals(""))
            return Result.fail("name不能为空，保存失败");
        boolean flag = projectService.save(project);
        if (flag) {
            redisUtil.flushByX("project");
            return Result.succ(project);
        }
        return Result.fail("保存失败");
    }

    @PostMapping("/delete")
    @Transactional
    public Result delete(@RequestParam Long id) {
        List<Work> works = workService.list();
        List<WorkContent> workContents = workContentService.list();
        List<WorkProve> workProves = workProveService.list();
        List<WorkRegister> workRegisters = workRegisterService.list();
        for (Work k: works) {
            if (k.getProjectId() == id) {
                k.setProjectId(Long.valueOf(0));
                workService.updateById(k);
            }
        }
        for (WorkContent t: workContents) {
            if (t.getProjectId() == id) {
                t.setProjectId(Long.valueOf(0));
                workContentService.updateById(t);
            }
        }
        for (WorkProve e: workProves) {
            if (e.getProjectId() == id) {
                e.setProjectId(Long.valueOf(0));
                workProveService.updateById(e);
            }
        }
        for (WorkRegister r: workRegisters) {
            if (r.getProjectId() == id) {
                r.setProjectId(Long.valueOf(0));
                workRegisterService.updateById(r);
            }
        }
        redisUtil.flushByX("work", "workContent", "workProve", "workRegister");
        boolean flag = projectService.removeById(id);
        if (flag) {
            redisUtil.flushByX("project");
            return Result.succ("删除成功");
        }
        return Result.fail("删除失败");
    }

    @PostMapping("/update")
    @Transactional
    public Result update(@RequestBody Project project) {
        if ((project.getName() == null || project.getName().equals("")) && (project.getMemo() == null || project.getMemo().equals("")))
            return Result.fail("project为空，更新失败");
        if (project.getName() == null || project.getName().equals(""))
            return Result.fail("name不能为空，更新失败");
        boolean flag = projectService.updateById(project);
        if (flag) {
            redisUtil.flushByX("project");
            return Result.succ(project);
        }
        return Result.fail("修改失败");
    }
}
